var RESULT = null;
var TEMP = null;

function logout(){
	return confirm("Are you sure to logout?");
}
function delConfirm(){
	return confirm("Are you sure to delete operation?");
}

function validLogin(){
	var tmp = $('#id').val();
	if (tmp == null || tmp == '') {
		alert('Please input your account');
		return false;
	} else {
		if (!/\w{4,15}/.test(tmp)) {
			alert('Account\'s length at least 4, most 15 single word');
			return false;
		}
	}
	tmp = $('#pass').val();
	if (tmp == null || tmp == '') {
		alert('Please input your password');
		return false;
	}else {
		if (!/\w{6,18}/.test(tmp)) {
			alert('Password\'s length at least 6, most 18 single word');
			return false;
		}
	}
	tmp = $('#scode').val();
	if (tmp == null || tmp == '') {
		alert('Please input security code');
		return false;
	}else {
		if (!/\w{4,4}/.test(tmp)) {
			alert('security code\'s length only 4 single word');
			return false;
		}
	}
	
	if (RESULT == 'FAILED') {
		alert('Sorry, security code is incorrect');
		return false;
	}
	
	return true;
}

function index(){
	obj = $('#scode');
	obj.focus(function(){
		$('span.hscode').fadeIn("slow");
		$('span.word').hide();
	});
	obj.blur(function(){
		checkScode();
	});
	$('#refreshScode').click(function(){
		$(this).blur();
		$('#sscode').attr('src', BASE + '/sCode?token' + new Date().getTime());
	});
}

function register(){
	$('#id').blur(function(){
		checkUserExist();
	});
	index();
}

function init(){
	$('a[tg="main"]').not("#del").click(
		function(){
			clearActive();
			$(this).blur();
			$(this).addClass('active');
			return beforeLoad($(this));
		}
	); 
}
function clearActive(){
	$('ul.menu li a').not("#stop").each(
		function(i){
			$(this).removeClass('active');
		}
	);
}
function page(){
	$('a[sg="main"]').not("#del").click(
		function(){
			return beforeLoad($(this));
		}
	); 
	$('a[id="del"]').click(
		function() {
			if (delConfirm()) {
				type = $(this).attr('type');
				if (type == 'user') {
					TEMP = $(this).attr('seq');
					postDeleteUser({id: $(this).attr('uid')}, cbDeleteUser);
				} else {
					alert('type:' + type + ' is unkonw');
				}
			}
		}
	);
}
function searchUser(){
	var val = $('#search').val();
	if (null == val || '' == val) {
		alert('Please input search value');
		return false;
	} 
	var search = $('#searchForm')
	var attr = search.attr('dist');
	search.attr('dist',attr + '?id=' + val);
	beforeLoad(search);
	return false;
}
function beforeLoad(o) {
	loading();
	return loadUrl(BASE + $(o).attr('dist'));
}
function loading() {
	$('#main').html("<img src='" + BASE + "/resources/images/loading.gif'/> Loading...");
}
function loadUrl(url){
	var i = url.indexOf('?');
	if (i > -1) {
		url += '&';
	} else {
		url += '?'
	}
	$('#main').load(url + 'token=' + new Date().getTime());
	i = null;
	return false;
}

//---------- validation ------------------------
function validAddorUpdateUser(b) {
	var tmp;
	if (b) {
		tmp = $('#id').val();
		if (null == tmp || '' == tmp) {
			alert('Please input account');
			return false;
		} else {
			if (!/\w{4,15}/.test(tmp)) {
				alert('Account\'s length at least 6, most 18 single word.');
				return false;
			}
			//checkUserExist();
			if (RESULT == 'FAILED') {
				alert('Account has been registed, please change another');
				return false;
			}
		}
		tmp = $('#pass').val();
		if (null == tmp || '' == tmp) {
			alert('Please input password');
			return false;
		} else {
			if (!/\w{6,18}/.test(tmp)) {
				alert('Password\'s length at least 6, most 18 single word.');
				return false;
			}
		}
		tmp = $('#confpass').val();
		if (null == tmp || '' == tmp) {
			alert('Please input confirm password');
			return false;
		} else {
			if (!/\w{6,18}/.test(tmp)) {
				alert('Confirm password\'s length at least 6, most 18 single word.');
				return false;
			}
		}
		if ($('#pass').val() != $('#confpass').val()) {
			alert('password and confirm password not same')
			return false;
		}
	}
	tmp = $('#group').val();
	if (null == tmp || '' == tmp) {
		alert('Please select group');
		return false;
	}
	tmp = $('#limit').val();
	if (null == tmp || '' == tmp) {
		alert('Please input limit date');
		return false;
	}
	
	var data = $('#userForm').serialize();
	if (b) postAddUser(data, cbAddUser);
	else postUpdateUser(data, cbUpdateUser);
	return false;
}

function validUpdateUserPwd(b) {
	var tmp;
	if (b) {
		tmp = $('#oldpass').val();
		if (null == tmp || '' == tmp) {
			alert('Please input old password');
			return false;
		} else {
			if (!/\w{6,18}/.test(tmp)) {
				alert('Password\'s length at least 6, most 18 single word.');
				return false;
			}
		}
	}
	tmp = $('#pass').val();
	if (null == tmp || '' == tmp) {
		alert('Please input new password');
		return false;
	} else {
		if (!/\w{6,18}/.test(tmp)) {
			alert('Password\'s length at least 6, most 18 single word.');
			return false;
		}
	}
	tmp = $('#confpass').val();
	if (null == tmp || '' == tmp) {
		alert('Please input confirm password');
		return false;
	} else {
		if (!/\w{6,18}/.test(tmp)) {
			alert('Confirm password\'s length at least 6, most 18 single word.');
			return false;
		}
	}
	if ($('#pass').val() != $('#confpass').val()) {
		alert('password and confirm password not same')
		return false;
	}
	
	if (b) {
		if ($('#oldpass').val() == $('#pass').val()) {
			alert('New password and old password could not the same')
			return false;
		}
	}
	
	var data = $('#userForm').serialize();
	if (!b) {
		postUpdateUserPwdByAdmin(data, cbUpdateUserPwd);
	} else {
		postUpdateUserPwdByUser(data, cbUpdateUserPwd);
	}
	
	return false;
}

function vaildRegisterUser() {
	var tmp;
	tmp = $('#id').val();
	if (null == tmp || '' == tmp) {
		alert('Please input account');
		return false;
	} else {
		if (!/\w{4,15}/.test(tmp)) {
			alert('Account\'s length at least 6, most 18 single word.');
			return false;
		}
		//checkUserExist();
		if (RESULT == 'FAILED') {
			alert('Account has been registed, please change another');
			return false;
		}
	}
	tmp = $('#pass').val();
	if (null == tmp || '' == tmp) {
		alert('Please input password');
		return false;
	} else {
		if (!/\w{6,18}/.test(tmp)) {
			alert('Password\'s length at least 6, most 18 single word.');
			return false;
		}
	}
	tmp = $('#confpass').val();
	if (null == tmp || '' == tmp) {
		alert('Please input confirm password');
		return false;
	} else {
		if (!/\w{6,18}/.test(tmp)) {
			alert('Confirm password\'s length at least 6, most 18 single word.');
			return false;
		}
	}
	if ($('#pass').val() != $('#confpass').val()) {
		alert('password and confirm password not same')
		return false;
	}
	
	var data = $('#registForm').serialize();
	postRegisterUser(data, cbRegisterUser);
	
	return false;
}

//---------- ajax ------------------------

function checkScode() {
	var val = $('#scode').val();
	if ('' == val) {
		$('#okButton').attr('disabled', 'disabled');
		$('#scode').removeClass('no').removeClass('yes');
		return;
	}
	if (!/\w{4,4}/.test(val)) {
		$('#okButton').attr('disabled', 'disabled');
		alert('Scurity code\'s length only 4 single word');
	} else {
		postCheckScode({scode: val}, cbCheckScode);
	}
	val = null;
}
function cbCheckScode(resp) {
	RESULT = resp.returnCode;
	if (resp.returnCode != 'NORMAL') {
		//alert('Sorry, security code is incorrect');
		cbCleanCheckScode(false);
	} else {
		cbCleanCheckScode(true);
	}
}
function cbCleanCheckScode(b) {
	obj = $('#scode');
	if(b) {
		$('#okButton').attr('disabled', '');
		obj.removeClass('no').addClass('yes');
	} else {
		$('#okButton').attr('disabled', 'disabled');
		obj.removeClass('yes').addClass('no');	
	}
	
}

function checkUserExist() {
	var val = $('#id').val();
	if ('' == val) return;
	if (!/\w{4,15}/.test(val)) {
		alert('Account\'s length at least 6, most 18 single word.');
	} else {
		postCheckUser({id: val}, cbCheckUserExist);
	}
	val = null;
}
function cbCheckUserExist(resp) {
	var check=$('span.check');
	RESULT = resp.returnCode;
	if (resp.returnCode == 'NORMAL') {
		check.removeClass('no').addClass('yes').html("Congratulations");
	} else {
		check.removeClass('yes').addClass('no').html("Has been registed");
	}
	check=null;
}

function cbAddUser(resp) {
	if (resp.returnCode == 'NORMAL') {
		alert('Congratulations, create user success')
		cbCleanAddUserForm(true);
	} else {
		alert('Sorry, create user failed, try a little late');
	}
}
function cbCleanAddUserForm(b) {
	if (b) {
		$('#id').val('');
		$('span.check').removeClass('yes').removeClass('no').html('');
		$('#pass').val('');
		$('#confpass').val('');
		$('#group').attr('value','');
		//$('#limit').val();
	}
}

function cbUpdateUser(resp) {
	if (resp.returnCode == 'NORMAL') {
		alert('Congratulations, update user success')
	} else {
		alert('Sorry, update user failed, try a little late');
	}
}
function cbDeleteUser(resp) {
	if (resp.returnCode == 'NORMAL') {
		alert('Congratulations, delete user success')
		cbCleanDeleteUserForm();
	} else {
		alert('Sorry, delete user failed, try a little late');
	}
}
function cbCleanDeleteUserForm() {
	$('tr[seq="'+ TEMP +'"]').remove();
	$('tr[seq^="seq"]').each(function(i){
		i % 2 == 0 ? $(this).addClass('odd') : $(this).removeClass('odd');
	});
}
function cbUpdateUserPwd(resp) {
	if (resp.returnCode == 'NORMAL') {
		alert('Congratulations, update password success');
		cbCleanUpdateUserPwdForm();
	} else if(resp.returnCode == 'FAILED' && null != resp.errorMessage) {
		alert(resp.errorMessage);
		$('#oldpass').val('');
	} else { 
		alert('Sorry, update password failed, try a little late');
	}
}

function cbCleanUpdateUserPwdForm() {
	$('#oldpass').val('');
	$('#pass').val('');
	$('#confpass').val('');
}

function cbRegisterUser(resp) {
	if (resp.returnCode == 'NORMAL') {
		alert('Congratulations, register success');
		window.location = BASE;
	} else {
		alert('Sorry, register failed, try a little late');
	}
}
